package org.esprit.ocm.server.metier.oca.impl;

import java.util.ArrayList;
import java.util.List;

import org.esprit.ocm.client.rpc.oca.HostOcaService;
import org.esprit.ocm.model.oca.HostOca;
import org.esprit.ocm.server.metier.oca.api.Client;
import org.esprit.ocm.server.metier.oca.api.OneResponse;
import org.esprit.ocm.server.metier.oca.api.host.Host;
import org.esprit.ocm.server.metier.oca.api.host.HostPool;

public class HostOcaServiceImpl implements HostOcaService {

	/**
	 * List All hosts
	 * 
	 * @param client
	 * @return
	 * @throws Exception
	 */
	public List<HostOca> listAll(String secret, String endpoint) throws Exception {
		Client client = new Client(secret, endpoint);
		HostPool pool = new HostPool(client);
		pool.info();

		List<HostOca> l = new ArrayList<HostOca>();

		for (Host from : pool) {
			HostOca to = new HostOca();

			to.setId(from.xpath("ID"));
			to.setName(from.xpath("NAME"));
			to.setState(from.xpath("STATE"));
			to.setIm_mad(from.xpath("IM_MAD"));
			to.setVm_mad(from.xpath("VM_MAD"));
			to.setTm_mad(from.xpath("TM_MAD"));
			to.setLast_mon_time(from.xpath("LAST_MON_TIME"));
			to.setCluster(from.xpath("CLUSTER"));

			to.setHost_share_hid(from.xpath("HOST_SHARE/HID"));
			to.setHost_share_disk_usage(from.xpath("HOST_SHARE/DISK_USAGE"));
			to.setHost_share_mem_usage(from.xpath("HOST_SHARE/MEM_USAGE"));
			to.setHost_share_cpu_usage(from.xpath("HOST_SHARE/CPU_USAGE"));
			to.setHost_share_max_disk(from.xpath("HOST_SHARE/MAX_DISK"));
			to.setHost_share_max_mem(from.xpath("HOST_SHARE/MAX_MEM"));
			to.setHost_share_max_cpu(from.xpath("HOST_SHARE/MAX_CPU"));
			to.setHost_share_free_disk(from.xpath("HOST_SHARE/FREE_DISK"));
			to.setHost_share_free_mem(from.xpath("HOST_SHARE/FREE_MEM"));
			to.setHost_share_free_cpu(from.xpath("HOST_SHARE/FREE_CPU"));
			to.setHost_share_used_disk(from.xpath("HOST_SHARE/USED_DISK"));
			to.setHost_share_used_mem(from.xpath("HOST_SHARE/USED_MEM"));
			to.setHost_share_used_cpu(from.xpath("HOST_SHARE/USED_CPU"));
			to.setHost_share_running_vms(from.xpath("HOST_SHARE/RUNNING_VMS"));

			to.setTemplate_cpuspeed(from.xpath("TEMPLATE/CPUSPEED"));
			to.setTemplate_freecpu(from.xpath("TEMPLATE/FREECPU"));
			to.setTemplate_freememory(from.xpath("TEMPLATE/FREEMEMORY"));
			to.setTemplate_hostname(from.xpath("TEMPLATE/HOSTNAME"));
			to.setTemplate_hypervisor(from.xpath("TEMPLATE/HYPERVISOR"));
			to.setTemplate_name(from.xpath("TEMPLATE/NAME"));
			to.setTemplate_totalcpu(from.xpath("TEMPLATE/TOTALCPU"));
			to.setTemplate_totalmemory(from.xpath("TEMPLATE/TOTALMEMORY"));
			to.setTemplate_usedcpu(from.xpath("TEMPLATE/USEDCPU"));
			to.setTemplate_usedmemory(from.xpath("TEMPLATE/USEDMEMORY"));

			to.setErrorMSG(from.info().getErrorMessage());
			l.add(to);
		}
		return l;
	}

	/**
	 * Describe Host
	 * 
	 * @param client
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public HostOca info(String secret, String endpoint, int id) throws Exception {
		Client client = new Client(secret, endpoint);
		Host from = new Host(id, client);

		HostOca to = new HostOca();

		to.setId(from.xpath("ID"));
		to.setName(from.xpath("NAME"));
		to.setState(from.xpath("STATE"));
		to.setIm_mad(from.xpath("IM_MAD"));
		to.setVm_mad(from.xpath("VM_MAD"));
		to.setTm_mad(from.xpath("TM_MAD"));
		to.setLast_mon_time(from.xpath("LAST_MON_TIME"));
		to.setCluster(from.xpath("CLUSTER"));

		to.setHost_share_hid(from.xpath("HOST_SHARE/HID"));
		to.setHost_share_disk_usage(from.xpath("HOST_SHARE/DISK_USAGE"));
		to.setHost_share_mem_usage(from.xpath("HOST_SHARE/MEM_USAGE"));
		to.setHost_share_cpu_usage(from.xpath("HOST_SHARE/CPU_USAGE"));
		to.setHost_share_max_disk(from.xpath("HOST_SHARE/MAX_DISK"));
		to.setHost_share_max_mem(from.xpath("HOST_SHARE/MAX_MEM"));
		to.setHost_share_max_cpu(from.xpath("HOST_SHARE/MAX_CPU"));
		to.setHost_share_free_disk(from.xpath("HOST_SHARE/FREE_DISK"));
		to.setHost_share_free_mem(from.xpath("HOST_SHARE/FREE_MEM"));
		to.setHost_share_free_cpu(from.xpath("HOST_SHARE/FREE_CPU"));
		to.setHost_share_used_disk(from.xpath("HOST_SHARE/USED_DISK"));
		to.setHost_share_used_mem(from.xpath("HOST_SHARE/USED_MEM"));
		to.setHost_share_used_cpu(from.xpath("HOST_SHARE/USED_CPU"));
		to.setHost_share_running_vms(from.xpath("HOST_SHARE/RUNNING_VMS"));

		to.setTemplate_cpuspeed(from.xpath("TEMPLATE/CPUSPEED"));
		to.setTemplate_freecpu(from.xpath("TEMPLATE/FREECPU"));
		to.setTemplate_freememory(from.xpath("TEMPLATE/FREEMEMORY"));
		to.setTemplate_hostname(from.xpath("TEMPLATE/HOSTNAME"));
		to.setTemplate_hypervisor(from.xpath("TEMPLATE/HYPERVISOR"));
		to.setTemplate_name(from.xpath("TEMPLATE/NAME"));
		to.setTemplate_totalcpu(from.xpath("TEMPLATE/TOTALCPU"));
		to.setTemplate_totalmemory(from.xpath("TEMPLATE/TOTALMEMORY"));
		to.setTemplate_usedcpu(from.xpath("TEMPLATE/USEDCPU"));
		to.setTemplate_usedmemory(from.xpath("TEMPLATE/USEDMEMORY"));

		to.setErrorMSG(from.info().getErrorMessage());

		to.setErrorMSG(from.info().getErrorMessage());
		return to;
	}

	/**
	 * Allocate new host
	 * 
	 * @param client
	 * @param hostname
	 * @param im
	 * @param vmm
	 * @param tm
	 * @return
	 * @throws Exception
	 */
	public String allocate(String secret, String endpoint, String hostname, String im, String vmm, String tm) throws Exception {
		Client client = new Client(secret, endpoint);
		OneResponse res = Host.allocate(client, hostname, im, vmm, tm);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * Delete a host
	 * 
	 * @param client
	 * @param hostId
	 * @throws Exception
	 */
	public String delete(String secret, String endpoint, int hostId) throws Exception {
		Client client = new Client(secret, endpoint);
		OneResponse res = Host.delete(client, hostId);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * Enable host
	 * 
	 * @param client
	 * @param hostId
	 * @return
	 * @throws Exception
	 */
	public String enable(String secret, String endpoint, int hostId) throws Exception {
		Client client = new Client(secret, endpoint);
		OneResponse res = Host.enable(client, hostId, true);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * disable host
	 * 
	 * @param client
	 * @param hostId
	 * @return
	 * @throws Exception
	 */
	public String disable(String secret, String endpoint, int hostId) throws Exception {
		Client client = new Client(secret, endpoint);
		OneResponse res = Host.enable(client, hostId, false);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}
}
